import { _decorator, Component, Node } from 'cc';
import { MoveCtrl } from '../Controller/MoveCtrl';
import { StateCtrl } from '../Controller/StateCtrl';
import { Emitter } from '../../FrameWork/Common/Emitter';
const { ccclass, property } = _decorator;

@ccclass('RoleBase')
export class RoleBase extends Component {
    //创建一个emitter对象，用于控制器之间的消息传递
    protected _emitter: Emitter = null;
    get emitter() {
        if (!this._emitter) {
            this._emitter = new Emitter();
        }
        return this._emitter;
    }
    protected _moveCtrl: MoveCtrl = null;
    protected _stateCtrl: StateCtrl = null;
    protected _speed: number = null;
    get speed() {
        return this._speed;
    }
    set speed(speed: number) {
        this._speed = speed;
    }

    start() {
        this._init();
    }

    protected _init() {
        this._onInit();
        this._moveCtrl = this.node.addComponent(MoveCtrl);
        this._moveCtrl.init(this);
        this._stateCtrl = this.node.addComponent(StateCtrl);
        this._stateCtrl.init(this);
    }

    //供派生类重写
    protected _onInit() {

    }
    //供派生类重写
    onUpdate(dt: number) { }

    protected update(dt: number): void {
        this.onUpdate(dt);
        this._moveCtrl?.onUpdate(dt);
        this._stateCtrl?.onUpdate(dt);
    }
}


